#!/usr/bin/env python

from __future__ import print_function

import subprocess
import sys


def run_command(cmd):
    try:
        output = subprocess.check_output(
            cmd, stderr=subprocess.STDOUT, shell=True, universal_newlines=True)
        return output

    except subprocess.CalledProcessError as e:
        print("Could not run command `%s`: %s" % (cmd, e.output))
        return None


revision = ""
if len(sys.argv) == 2:
    revision = sys.argv[1]
if len(sys.argv) > 2:
    print("Usage: %s [revision]")
    quit()

cmd = "git diff --diff-filter=ACMR --oneline --name-only %s  -- '*.py'" % revision
file_list = run_command(cmd)

if len(file_list) == 0:
    print("No changes on current working dir and no revision specified. Nothing to do")
else:
    # TODO Maybe use as a module, so we can output which files were changed?
    cmd = "yapf -i %s" % (file_list.replace("\n", " "))
    run_command(cmd)
